package com.ynet.middleground.contract.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObj;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.contract.bean.AddContractTypeReq;
import com.ynet.middleground.contract.bean.DeleteContractTypeReq;
import com.ynet.middleground.contract.bean.QueryContractTypeBaseReqObj;
import com.ynet.middleground.contract.bean.UpdateContractTypeReq;
import com.ynet.middleground.contract.dto.ContractTypeDTO;
import com.ynet.middleground.contract.model.ContractTypeModel;
import com.ynet.middleground.contract.service.ContractTypeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 合约类型服务实现类
 * @date 2020-04-06 03:58
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "合约类型管理服务", tags = {"合约类型管理服务"})
@Path("contract-type")
@Service(timeout = 6000, version = "0.0.1")
public class ContractTypeServiceImpl implements ContractTypeService, GenericService {

    @Autowired
    ContractTypeModel contractTypeModel;

    /**
     * 查询合约类型列表
     *
     * @param baseReqObj 基础请求类
     * @return 合约类型信息列表
     */
    @POST
    @Path("list")
    @ApiOperation(value = "查询合约类型列表", notes = "查询当前渠道所有合约类型列表")
    @ApiImplicitParam(name = "requestObj", dataType = "BaseReqObj", paramType = "body", required = true)
    @Override
    public ServiceResult<List<ContractTypeDTO>> list(@Valid BaseReqObj baseReqObj) {
        ServiceResult<List<ContractTypeDTO>> result = new ServiceResult<>();
        try {
            result.setResult(contractTypeModel.listContractType(baseReqObj));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询合约类型列表服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 查询合约类型列表（可使用业态公司 id 过滤）
     * 
     * @param queryContractTypeBaseReqObj 查询合约类型列表请求信息
     * @return
     */
    @POST
    @Path("list-with-format")
    @ApiOperation(value = "查询合约类型列表（可用业态公司过滤）", notes = "查询当前渠道所有合约类型列表（仅供综合管理平台使用）")
    @ApiImplicitParam(name = "requestObj", dataType = "QueryContractTypeBaseReqObj", paramType = "body",
        required = true)
    @Override
    public ServiceResult<List<ContractTypeDTO>>
        listWithFormat(QueryContractTypeBaseReqObj queryContractTypeBaseReqObj) {
        ServiceResult<List<ContractTypeDTO>> result = new ServiceResult<>();
        try {
            result.setResult(contractTypeModel.listContractType(queryContractTypeBaseReqObj));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询合约类型列表服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 新增合约类型
     *
     * @param addContractTypeReq 新增合约类型请求类
     * @return 合约类型 id
     */
    @POST
    @ApiOperation(value = "新增合约类型", notes = "新增合约类型信息<br/>合约类型名称、合约类型编码均不能重复")
    @ApiImplicitParam(name = "requestObj", dataType = "AddContractTypeReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Integer> add(@Valid AddContractTypeReq addContractTypeReq) {
        ServiceResult<Integer> result = new ServiceResult<>();
        try {
            result.setResult(contractTypeModel.addContractType(addContractTypeReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "新增合约类型服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 删除合约类型
     *
     * @param deleteContractTypeReq 删除合约类型请求类
     * @return 结果标志，以 success 字段值标识 true/false
     */
    @DELETE
    @ApiOperation(value = "删除合约类型", notes = "删除合约类型信息<br/>合约类型被模板使用过或合约类型编码被生成过合约编号不可删除")
    @ApiImplicitParam(name = "requestObj", dataType = "DeleteContractTypeReq", paramType = "body", required = true)
    @Override
    public ServiceResult delete(@Valid DeleteContractTypeReq deleteContractTypeReq) {
        ServiceResult result = new ServiceResult<>();
        try {
            contractTypeModel.deleteContractType(deleteContractTypeReq);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "删除合约类型服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 更新合约类型
     *
     * @param updateContractTypeReq 更新合约类型信息请求类
     * @return 结果标志，以 success 字段值标识 true/false
     */
    @PUT
    @ApiOperation(value = "更新合约类型", notes = "更新合约类型信息<br/>合约类型被模板使用过或合约类型编码被生成过合约编号不可修改")
    @ApiImplicitParam(name = "requestObj", dataType = "UpdateContractTypeReq", paramType = "body", required = true)
    @Override
    public ServiceResult update(@Valid UpdateContractTypeReq updateContractTypeReq) {
        ServiceResult result = new ServiceResult<>();
        try {
            contractTypeModel.updateContractType(updateContractTypeReq);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "更新合约类型服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

}
